package com.biz.aoao.mysql.mysqlbinlogsync.config;

import com.biz.aoao.mysql.mysqlbinlogsync.extra.ConsoleSyncInfoSetting;
import com.sun.tools.javac.util.StringUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @Author scgaopan
 * @Date 2020/8/5 19:54
 * @Version 1.0
 * @Desc   mysql配置数列表
 */
@Configuration
@ConfigurationProperties(prefix = "mysql.binlog")
@Data
public class MySqlHostLists {
    //mysql配置主机列表
    private List<MySqlHost> hosts;

    @Autowired
    private ConsoleSyncInfoSetting consoleSyncInfoSetting;
    /**
     * 根据hostname获取对应的Host配置信息
     * @param serverid
     * @return
     */
    public MySqlHost getMysqlHostByServerId(Long serverid){
        if(serverid==null){
            return null;
        }
        MySqlHost mySqlHost = hosts.stream().filter(o-> serverid.equals(o.getServerid())).findAny()
              .orElse(null);
        if(mySqlHost!=null){
            BinLogPositionInfo currentBinLogPositonInfo = consoleSyncInfoSetting.getCurrentBinLogPositonInfo(serverid);
            mySqlHost.setFilename(currentBinLogPositonInfo.getFileName());
            mySqlHost.setPosition(currentBinLogPositonInfo.getPositon());
        }
        return  mySqlHost;
    }
}
